package com.nosnow.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nosnow.common.utils.R;
import com.nosnow.edu.entity.EduCourse;
import com.nosnow.edu.entity.vo.CourseInfoVo;
import com.nosnow.edu.entity.vo.CoursePublishVo;
import com.nosnow.edu.entity.vo.CourseQuery;
import com.nosnow.edu.service.EduCourseService;
import com.nosnow.handler.SnowflakeException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author bingheng
 * @since 2020-04-04
 */
@Api(description = "课程基本信息")
@RestController
@RequestMapping("/edu/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @ApiOperation(value="添加课程基本信息")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String courseId = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", courseId);
    }

    @ApiOperation(value="根据课程Id查询课程基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    @ApiOperation(value="根据课程Id修改课程基本信息")
    @PostMapping("modifyCourseInfo")
    public R modifyCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value="根据课程Id查询课程确认信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    @ApiOperation(value="课程最终发布-修改课程发布状态")
    @GetMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        // TODO 视频发布的状态可以优化到常量类中，然后获取
        eduCourse.setStatus("Normal");
        boolean flog = eduCourseService.updateById(eduCourse);
        if(flog) {
            return R.ok();
        } else{
            return R.error();
        }
    }

    /**
     * TODO 该条件分页列表查询待优化：列表中需增加该课程的讲师，查询条件也应该增加讲师，涉及到多表联查的分页
     * @param current
     * @param limit
     * @param courseQuery
     * @return
     */
    @ApiOperation(value="课程列表分页条件查询")
    @PostMapping("pageEduCourse/{current}/{limit}")
    public R pageEduCourse(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                           @ApiParam(name = "limit", value = "每页记录数", required = true)  @PathVariable long limit,
                           @RequestBody(required = false)CourseQuery courseQuery) {
        try {
            // 1. 创建Page对象
            Page<EduCourse> page = new Page<>();

            // 2. 构建多条件组合查询，判断条件值是否为空，如果不为空则动态拼接（原理：mybatis 动态sql）
            QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

            // 3. 如果课程的标题不为空，则拼接课程标题作为模糊查询的条件
            if(!StringUtils.isEmpty(courseQuery.getTitle())){
                wrapper.like("title", courseQuery.getTitle());
            }

            // 4. 如果课程的状态不为空，则拼接课程状态作为模糊查询的条件
            if(!StringUtils.isEmpty(courseQuery.getStatus())) {
                wrapper.like("status", courseQuery.getStatus());
            }

            // 5. 添加时间排序
            wrapper.orderByDesc("gmt_modified");

            // 6. 调用方法实现条件分页查询, mp底层封装，把分页的所有数据封装到 page 对象里面
            eduCourseService.page(page, wrapper);

            // 7. 采用链式编程，将结果返回
            return R.ok().data("total", page.getTotal()).data("rows", page.getRecords());
        } catch (Exception e) {
            throw new SnowflakeException(20001, "等会再点我好么，我要回家吃饭...");
        }

    }

    /**
     * 删除课程的逻辑：删除该课程后，同时删除该课程下的所有信息
     * @param courseId
     * @return
     */
    @ApiOperation(value="根据课程Id删除课程")
    @DeleteMapping("removeCourse/{courseId}")
    public R removeCourse(@PathVariable String courseId) {
        Boolean flog = eduCourseService.deleteCourse(courseId);
        if(flog) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

